﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public partial class frmDrugsHelper : BaseWinFrom
{
    TextBox txtBox;
    Model.Diagnosis Diagnosis;

    public frmDrugsHelper(TextBox HelpTextBox,Model.Diagnosis diagnosis )
    {
        if (HelpTextBox == null)
        {
            this.Dispose();
        }
        else
        {
            InitializeComponent();
        }
        txtBox = HelpTextBox;
        txtBox.Click += new EventHandler(txtBox_Click);
        Diagnosis = diagnosis;

        Model.Search.DrugsList dl = new Model.Search.DrugsList
        {
            PageSize = int.MaxValue,
            Status = "1",
        };

        foreach (Model.Drugs i in new RST.Drugs().List(dl))
        {
            this.Controls.Add(new UC_DrugsCell(i, diagnosis));
        }
        Button btOk = new Button();
        btOk.Text = "确定";
        btOk.Click += new EventHandler(btOk_Click);
        this.Controls.Add(btOk);
        this.AcceptButton = btOk;
        OnResize(null);
    }

    void txtBox_Click(object sender, EventArgs e)
    {
        this.Show();
        this.Visible = true;
    }

    protected override void OnClosing(CancelEventArgs e)
    {
        this.Visible = false;
        e.Cancel = true;
        base.OnClosing(e);
    }

    void btOk_Click(object sender, EventArgs e)
    {
        StringBuilder sb = null;
        Diagnosis.DiagnosisDrugs = new List<Model.DiagnosisDrugs>();
        foreach (Control i in Controls)
        {
            UC_DrugsCell o = i as UC_DrugsCell;
            if (o == null)
            {
                continue;
            }
            string v = o.GetDiagnosisDrugsString();
            if (v == null)
            {
                continue;
            }
            if (sb == null)
            {
                sb = new StringBuilder();
            }
            else
            {
                sb.Append(Model.DiagnosisDrugs.TAGSplitPairsGrounp);
            }
            Diagnosis.DiagnosisDrugs.Add(o.GetDiagnosisDrugs());
            sb.Append(v);
        }
        if (sb != null)
        {
            sb.Append(Model.DiagnosisDrugs.TAGEND);
            txtBox.Text = sb.ToString();
        }
        else
        {
            txtBox.Text = null;
        }
        WinFromDisplay();
    }

    protected override void OnResize(EventArgs e)
    {
        int x = 0; int y = 0;
        foreach (Control o in Controls)
        {
            if (!string.IsNullOrEmpty(o.Name))
            {
                continue;
            }
            if (x + o.Width > this.Width)
            {
                y += o.Height;
                x = 0;
                o.Left = x;
                o.Top = y;
            }
            else
            {
                o.Left = x;
                o.Top = y;
            }
            x += o.Width;
        }
        base.OnResize(e);
    }

    void WinFromDisplay()
    {
        this.Visible = false;
        //txtBox.Click -= new EventHandler(txtBox_Click);
        //this.Dispose(true);
    }
}

public class UC_DrugsCell : Control
{
    public Model.Drugs Drugs;

    public Label lbDrugsName = new Label();
    public TextBox txtDrugsCount = new TextBox() { Width = 25 };

    public UC_DrugsCell(Model.Drugs drugs, Model.Diagnosis diagnosis)
    {
        Drugs = drugs;
        if (drugs != null)
        {
            this.Height = 30;

            lbDrugsName.Text = drugs.Name;
            lbDrugsName.TextAlign = ContentAlignment.MiddleRight;
            Size size = lbDrugsName.CreateGraphics().MeasureString(drugs.Name, lbDrugsName.Font).ToSize();
            lbDrugsName.Width = Math.Max(size.Width + 10, 80);
            lbDrugsName.Height = size.Height;
            lbDrugsName.Left = 0;
            lbDrugsName.Top = (this.Height - lbDrugsName.Height) / 2;
            txtDrugsCount.Left = lbDrugsName.Width;
            txtDrugsCount.Top = (this.Height - txtDrugsCount.Height) / 2;

            if (diagnosis != null && diagnosis.DiagnosisDrugs!=null)
            {
                foreach (var i in diagnosis.DiagnosisDrugs)
                {
                    if (drugs.Id == i.DrugId)
                    {
                        txtDrugsCount.Text = i.Number.ToString();
                        break;
                    }
                }
            }

            this.Controls.Add(lbDrugsName);
            this.Controls.Add(txtDrugsCount);
            this.Width = lbDrugsName.Width + txtDrugsCount.Width;
        }
    }

    public Model.DiagnosisDrugs GetDiagnosisDrugs()
    {
        int Count = 0;
        if (Drugs != null && !string.IsNullOrEmpty(txtDrugsCount.Text) && int.TryParse(txtDrugsCount.Text, out Count))
        {
            return new Model.DiagnosisDrugs
            {
                DrugId = Drugs.Id,
                Name = Drugs.Name,
                Number = Count,
                Status = 1,
            };
        }
        return null;
    }

    public string GetDiagnosisDrugsString()
    {
        int Count = 0;
        if (Drugs != null && !string.IsNullOrEmpty(txtDrugsCount.Text) && int.TryParse(txtDrugsCount.Text, out Count))
        {
            return new Model.DiagnosisDrugs { DrugId = Drugs.Id, Status = 1, Name = Drugs.Name, Number = Count }.ToString();
            //return string.Format("{0}{1}{2}", Drugs.Name, Model.DiagnosisDrugs.TAGSplitNameValue, Count);
        }
        return null;
    }
}